package cn.wingware.util;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayList;
import java.util.List;

public class Page implements Externalizable {
	public static boolean DATA_LOAD_MODE_ALL = true;
	public static boolean DATA_LOAD_MODE_SEGMENT = false;
	
	private int pageSize;
	private int allCount = 0;
	private int currentPage = 1;
	private int pageCount = 0;
	private boolean dataLoadMode = DATA_LOAD_MODE_ALL;
	private List<?> dataList;

	public Page() {
	}
	/**
	 * 全部加载数据分页
	 */
	public Page(int pageSize, int currentPage, List<?> dataList) {
		this.pageSize = pageSize;
		this.currentPage = (currentPage < 1) ? 1 : currentPage;
		this.dataLoadMode = DATA_LOAD_MODE_ALL;
		this.allCount = dataList!=null ? dataList.size() : 0;
		if(this.allCount > 0) {
			pageCount = (this.allCount + pageSize - 1)/pageSize;
			if(this.currentPage > pageCount && pageCount > 0){
				this.currentPage = pageCount;
			}
			int begin = this.pageSize*(this.currentPage-1)+1;
			int end = this.pageSize*this.currentPage;
			if(end > this.allCount)
				end = this.allCount;
			if(this.dataLoadMode == DATA_LOAD_MODE_ALL)
			this.dataList = dataList.subList(begin-1,end);
		} else if(this.allCount == 0 && dataList != null && !dataList.isEmpty()){
			this.dataList = new ArrayList<Object>();
		}
	}
	
	/**
	 * 部分加载数据分页
	 */
	public Page(int allCount,int pageSize, int currentPage,List<?> dataList) {
		this.pageSize = pageSize;
		this.currentPage = (currentPage < 1) ? 1 : currentPage;
		this.dataLoadMode = DATA_LOAD_MODE_SEGMENT;
		this.allCount = dataList!=null ? allCount : 0;
		if(this.allCount > 0) {
			pageCount = (this.allCount + pageSize - 1)/pageSize;
			if(this.currentPage > pageCount && pageCount > 0){
				this.currentPage = pageCount;
			}
			int begin = this.pageSize*(this.currentPage-1)+1;
			int end = this.pageSize*this.currentPage;
			if(end > this.allCount)
				end = this.allCount;
			this.dataList = dataList;
		} else if(this.allCount == 0 && dataList != null && !dataList.isEmpty()) {
			this.dataList = new ArrayList<Object>();
		}
	}

    //当序列化对象时,该方法自动调用   
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeObject(pageSize);
        out.writeObject(allCount);
        out.writeObject(currentPage);
        out.writeObject(pageCount);
        out.writeObject(dataList);
    }

    //当反序列化对象时,该方法自动调用
    public void readExternal(ObjectInput in) throws IOException,ClassNotFoundException {
        this.pageSize=(Integer)in.readObject();
        this.allCount=(Integer)in.readObject(); 
        this.currentPage=(Integer)in.readObject(); 
        this.pageCount=(Integer)in.readObject(); 
        this.dataList=(List<?>)in.readObject();  
    }
	
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getAllCount() {
		return allCount;
	}
	public void setAllCount(int allCount) {
		this.allCount = allCount;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	public List<?> getDataList() {
		return dataList;
	}
	public void setDataList(List<?> dataList) {
		this.dataList = dataList;
	}
    public String toString() {
    	StringBuffer sb=new StringBuffer();
    	sb.append("pageSize: ").append(this.pageSize).append("; ");
    	sb.append("allCount: ").append(this.allCount).append("; ");
    	sb.append("currentPage: ").append(this.currentPage).append("; ");
    	sb.append("pageCount: ").append(this.pageCount).append("; ");
    	sb.append("dataList: ").append(this.dataList).append("; ");
    	return sb.toString();
    }
}
